package main.Q301_400;

import java.util.Arrays;

public class Q321_330 {
    public static void main(String[] args) {
        System.out.println("Question321：");
        System.out.println("Question322：零钱兑换");
        System.out.println("Question323：");
        System.out.println("Question324：摆动排序Ⅱ");
        System.out.println("Question325：");
        System.out.println("Question326：3的幂");
        System.out.println("Question327：");
        System.out.println("Question328：奇偶链表");
        System.out.println("Question329：矩阵中的最长递增路径");
        System.out.println("Question330：");
    }
}

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

class Question322{
    public int process(int[] coins, int rem, int[] count) {
        if (rem < 0) return -1;
        if (rem == 0) return 0;
        if (count[rem - 1] != 0) return count[rem - 1];
        int min = Integer.MAX_VALUE;
        for (int coin : coins) {
            int res = process(coins, rem - coin, count);
            if (res >= 0 && res < min) {
                min = 1 + res;
            }
        }
        count[rem - 1] = (min == Integer.MAX_VALUE) ? -1 : min;
        return count[rem - 1];
    }
    public int coinChange(int[] coins, int amount) {
        if (amount<=0) return 0;
        int count=process(coins,amount,new int[amount]);
        return count;
    }
}

class Question324{
    public void wiggleSort(int[] nums) {
        int[] temp=new int[nums.length];
        int left= (nums.length-1)/2, right= nums.length-1,index=0;
        for (int i=0;i< nums.length;i++) temp[i]=nums[i];
        Arrays.sort(temp);
        for (int i=0;i< nums.length;i++){
            nums[i]= i%2==0 ? temp[left--] : temp[right--];
        }
    }
}

class Question326{
    public boolean isPowerOfThree(int n) {
        while (n != 0 && n % 3 == 0) {
            n /= 3;
        }
        return n == 1;
    }
}

class Question328{
    public ListNode oddEvenList(ListNode head) {
        if (head==null||head.next==null) return head;
        ListNode odd=head,even=head.next,evenHead=even;
        while (odd!=null&&even!=null&&odd.next!=null&&even.next!=null){
            odd.next=odd.next.next;
            even.next=even.next.next;
            odd=odd.next;
            even=even.next;
        }
        odd.next=evenHead;
        return head;
    }
}

class Question329{
    int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    int rows, columns;

    public int longestIncreasingPath(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        rows = matrix.length;
        columns = matrix[0].length;
        int[][] memory = new int[rows][columns];
        int ans = 0;
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < columns; ++j) {
                ans = Math.max(ans, process(matrix, i, j, memory));
            }
        }
        return ans;
    }

    public int process(int[][] matrix, int row, int column, int[][] memory) {
        if (memory[row][column] != 0) {
            return memory[row][column];
        }
        ++memory[row][column];
        for (int[] dir : dirs) {
            int newRow = row + dir[0], newColumn = column + dir[1];
            if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && matrix[newRow][newColumn] > matrix[row][column]) {
                memory[row][column] = Math.max(memory[row][column], process(matrix, newRow, newColumn, memory) + 1);
            }
        }
        return memory[row][column];
    }
}